package com.ftg.learn.bus.mapper;

import com.ftg.learn.bus.VO.MenuVo;
import com.ftg.learn.bus.entity.Menu;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 菜单表 Mapper 接口
 * </p>
 *
 * @author lalala
 * @since 2022-03-07
 */
public interface MenuMapper extends BaseMapper<Menu> {

    /**
     * 查询当前用户的权限
     *
     * @param uid 用户id
     * @return 当前用户所具有的菜单
     */
    @Select("select d.* from sys_user a\n" +
            "left join sys_user_role b on a.uid = b.uid\n" +
            "left join sys_role_menu c on b.rid = c.rid\n" +
            "left join sys_menu d on c.mid = d.mid " +
            "where a.uid = #{param1}")
    List<MenuVo> showMenuForUserId(Long uid);

    /**
     * 当前角色所有的菜单
     * @param rid
     * @return
     */
    @Select("select c.* from sys_role a\n" +
            "left join sys_role_menu b on a.rid = b.rid\n" +
            "left join sys_menu c on b.mid = c.mid " +
            "where a.rid = #{param1}")
    List<MenuVo> showMenuForRoleId(Long rid);

    /**
     * 给用户添加所有菜单id
     * @param rid 用户id
     * @param mid 菜单id
     * @return
     */
    @Insert("insert into sys_role_menu(rid,mid) values(#{rid},#{mid})")
    boolean saveOrUpdateMenusByRid(@Param("rid") Long rid, @Param("mid") Long mid);

    /**
     * 根据用户id删除用户下的菜单
     * @param rid
     * @return
     */
    @Delete("delete from sys_role_menu where rid = #{param1}")
    boolean deleteMenuByRid(Long rid);

    /**
     * 根据菜单id修改菜单名
     * @param mid 菜单id
     * @param title 菜单名
     * @return
     */
    @Update("update sys_menu set title = #{title} where mid = #{mid}")
    boolean updateForName(@Param("mid") Long mid, @Param("title") String title);


}
